<?php
//栏目分类设置
if (POWER != 5) {
  err('<script> alert("该命令必须以基本管理员身份登陆！请重登陆"); </script>');
}


function read_write($there_file, $here_file, $once_lenth) {
  if ($fp1 = fopen($there_file, 'rb')) {
    while (!feof($fp1)) {
      $text = fread($fp1, $once_lenth);
      if ($fp2 = @fopen($here_file, 'ab')) {
        fwrite($fp2, $text);
      } else {
        return false;
        break;
      }
    }
    if (feof($fp1)) {
      return true;
    }
    fclose($fp1);
    fclose($fp2);
  } else {
    err('读取远程升级包失败！');
  }
}

function freed_zip($file, $path) {
  if (class_exists('ZipArchive')) {
    $zip = new ZipArchive;
    if ($zip->open($file)) {
      $zip->extractTo($path);
      $zip->close();
      return true;
    }
  } else {
    $resource = @zip_open($file);
    while ($dir_resource = @zip_read($resource)) {
      if (@zip_entry_open($resource, $dir_resource)) {
        $file_name = $path.zip_entry_name($dir_resource);
        $file_path = substr($file_name, 0, strrpos($file_name, "/"));
        if(!is_dir($file_path)){
          @mkdir($file_path, 0777, true);
        }
        if(!is_dir($file_name)){
          $file_size = @zip_entry_filesize($dir_resource);
          $file_content = @zip_entry_read($dir_resource, $file_size);
          write_file_($file_name, $file_content);
        }
        @zip_entry_close($dir_resource);
      }
    }
    @zip_close($resource); 
    return true;
  }
  err('解压失败！可能是ZipArchive压缩类未开启！'.(PHP_VERSION < '5.3.0' ? '请尝试升级你的PHP版本（当前'.PHP_VERSION.'）到5.3以上（此版本以上默认支持）再试' : '').'');
}

function write_file_($file, $text) {
  if (!file_put_contents($file, $text)) {
    write_file($file, $text);
  }
}

















@ set_time_limit(0);  //若配置为 0 则不限定最久时间


$web['sitehttp'] = 'http://'.(!empty($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']).'/';  //站点网址
$web['root'] = get_root_domain($web['sitehttp']);

if (!$web['root'] || $web['root']=='localhost' || $web['root']=='127.0.0.1') {
  err('您当前域名不合法！无法进行进一步的升级操作');
}

if ($_GET && $_GET['for_zip'] == 1) {
  err('<p>连接下载中……请等候</p><iframe src="http://www.162100.com/s/upload.php?domain='.urlencode($web['root']).'&for_zip=1" allowtransparency="true" frameborder="0" scrolling="no" style="width:100%; height:60px;"></iframe>');
} else {

  if (!empty($_GET['del_zip_file'])) {
    $zip_file = preg_replace('/[\/]+|eval|base64_/i', '', $_GET['del_zip_file']);
    if (preg_match('/\.(zip|rar|gzip)$/i', $zip_file) && file_exists($zip_file)) {
      @unlink($zip_file);
    }
    alert('升级包已在根目录移除', '?get=upgrade');
  }

  echo '<p>连接下载中……请等候</p>';
  @ob_flush();
  @flush();
  @ require ('readonly/function/read_file.php');
  if ($remotely_file = read_file('http://www.162100.com/s/upload.php?domain='.urlencode($web['root']).'')) {
    echo '<p>读取远程信息成功！</p>';
    @ob_flush();
    @flush();
    if (preg_match('/\.(zip|rar|gzip)$/i', $remotely_file)) {
      echo '<p>识别远程升级包成功！</p>';
      @ob_flush();
      @flush();
      $local_file = basename($remotely_file);
      if (read_write($remotely_file, $local_file, 1024*8)) {
        echo '<p>下载远程升级包……请等候</p>';
        @ob_flush();
        @flush();
        if (file_exists($local_file)) {
          echo '<p>下载远程升级包成功！</p>';
          @ob_flush();
          @flush();
          @ require ('readonly/function/write_file.php');
          if (freed_zip($local_file, './')) {
            echo '<p>正在升级、文件更新中……请等候</p>';
            @ob_flush();
            @flush();
            err('<p><b class="redword">升级成功！</b></p><p class="grayword">是否删除升级包？<a href="?post=upgrade&del_zip_file='.urlencode($local_file).'">删除</a></p>');
          } else {
            err('<b class="blueword">升级失败！</b>原因：空间写权限不足');
          }
        } else {
          err('升级包没有正确生成！原因：空间写权限不足');
        }
      } else {
        err('<b class="blueword">下载远程升级包到主机硬盘失败！</b>原因：可能空间写权限不足');
      }
    } else {
      err($remotely_file);
    }
  } else {
    err('读取远程信息失败！');
  }

}






?>